import net.zjitc.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/11 8:36
 */
public class MybatisTEST {
    SqlSessionFactory  factory = null;
    @Before
    public void createFactory(){
        //读资源
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*构建会话工厂*/
         factory = new SqlSessionFactoryBuilder().build(inputStream);

    }
    @Test
    public void testSession(){

            /*从factory中获取session对象*/
            SqlSession session = factory.openSession();
            User user = session.selectOne("userTest.findById", 1);
            System.out.println(user);

    }

    @Test
    public void testDelete(){
        /*查询：
        * 可能修改数据库：*/

        /*从factory中获取session对象*/
        SqlSession session = factory.openSession();
        int num = session.delete("userTest.deleteById",4);
        session.commit();/*提交事务*/
        System.out.println(num);
    }

    @Test
    public void testFindAll(){
        /*查询：
         * 可能修改数据库：*/

        /*从factory中获取session对象*/
        SqlSession session = factory.openSession();
         List<User> list = session.selectList("userTest.findAll");

        System.out.println(list);
    }
}
